package ldh.im.web.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;

import ldh.common.PageResult;
import ldh.im.web.dao.RoleAuthorityDao;
import ldh.im.web.dao.RoleDao;
import ldh.im.web.exception.BusinessException;
import ldh.im.web.exception.DbErrorType;
import ldh.im.web.pojo.Authority;
import ldh.im.web.pojo.Role;
import ldh.im.web.pojo.RoleAuthority;
import ldh.im.web.pojo.where.RoleWhere;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author: ldh
 * @date: 2019-10-25 12:35:06
 */
@Service("roleService")
public class RoleService {

	@Resource
	private RoleDao roleDao;
	@Resource
	private RoleAuthorityDao roleAuthorityDao;

	@Transactional
	public Role insert(Role role) {
		int n = roleDao.insert(role);
		if (n != 1) {
			throw new RuntimeException("Role insert error!");
		}
		return role;
	}

	@Transactional
	public Role insertSelective(Role role) {
		int n = roleDao.insert(role);
		if (n != 1) {
			throw new RuntimeException("Role insertSelective error!");
		}
		return role;
	}

	@Transactional
	public Role updateById(Role role) {
		int  n = roleDao.updateById(role);
		if (n != 1) {
			throw new RuntimeException("Role update error!");
		}
		return role;
	}

	@Transactional
	public Role updateNotNullById(Role role) {
		int  n = roleDao.updateNotNullById(role);
		if (n != 1) {
			throw new RuntimeException("Role update error!");
		}
		return role;
	}


	@Transactional
	public void deleteById(Long id) {
		int n = roleDao.deleteById(id);
		if (n != 1) {
			throw new RuntimeException("Role delete error!");
		}
	}

	@Transactional(readOnly = true)
	public Role getById(Long id) {
		Role role = roleDao.getById(id);
		return role;
	}

	@Transactional(readOnly = true)
	public List<Role> getByIds(List<Long> roleIds) {
		if (roleIds == null || roleIds.size() < 1) return new ArrayList<>();
		return roleDao.getByIds(roleIds);
	}

	@Transactional(readOnly = true)
	public Role getByName(String name) {
		Role role = roleDao.getByName(name);
		return role;
	}

	@Transactional(readOnly = true)
	public Role getManyAuthoritysById(Long id) {
		Role role = roleDao.getManyAuthoritysById(id);
		return role;
	}

	@Transactional(readOnly = true)
	public Role getManyUsersById(Long id) {
		Role role = roleDao.getManyUsersById(id);
		return role;
	}

	@Transactional(readOnly = true)
	public List<Role> getAllRoles(RoleWhere roleWhere) {
		roleWhere.setPageSize(Integer.MAX_VALUE);
		return roleDao.findJoinByRoleWhere(roleWhere);
	}

	@Transactional(readOnly = true)
	public PageResult<Role> findByRoleWhere(RoleWhere roleWhere) {
		long total = roleDao.findTotalByRoleWhere(roleWhere);
		List<Role> imageList = new ArrayList<Role>();
		if (total > 0) {
			imageList  = roleDao.findByRoleWhere(roleWhere);
		}

		return new PageResult<Role>(roleWhere, total, imageList);
	}

	@Transactional(readOnly = true)
	public PageResult<Role> findJoinByRoleWhere(RoleWhere roleWhere) {
		long total = roleDao.findJoinTotalByRoleWhere(roleWhere);
		List<Role> imageList = new ArrayList<Role>();
		if (total > 0) {
			imageList  = roleDao.findJoinByRoleWhere(roleWhere);
		}

		return new PageResult<Role>(roleWhere, total, imageList);
	}

	@Transactional(readOnly = true)
	public long findTotalJoinByRoleWhere(RoleWhere roleWhere) {
		long total = roleDao.findJoinTotalByRoleWhere(roleWhere);
		return total;
	}

	@Transactional
	public void roleAuthorize(Role role, List<Authority> deleteAuthorityList, List<Authority> insertAuthorityList) {
		for(Authority authority : deleteAuthorityList) {
			int n = roleAuthorityDao.deleteByRoleIdAndAuthorityId(role.getId(), authority.getId());
			if (n != 1) throw new BusinessException(DbErrorType.Insert_Error, "roleAuthorize");
		}

		Date now = new Date();
		List<RoleAuthority> roleAuthorityList = new ArrayList<>();
		for(Authority authority : insertAuthorityList) {
			RoleAuthority roleAuthority = new RoleAuthority();
			roleAuthority.setAuthority(authority);
			roleAuthority.setRole(role);
			roleAuthority.setCreateTime(now);

			roleAuthorityList.add(roleAuthority);
		}

		if (roleAuthorityList.size() > 0) {
			int n = roleAuthorityDao.inserts(roleAuthorityList);
			if (n != roleAuthorityList.size()) throw new BusinessException(DbErrorType.Insert_Error, "roleAuthorize");
		}
	}
}
